﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using yncTech.BusinessObjects;

using EntitySpaces.Interfaces;

public partial class Admin_Fin_Fin006 : AdminBasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            txtStartDate.Value = DateTime.Now.AddMonths(-1).Date.ToString(ConstantValue.DateFormat);
            txtEndate.Value = DateTime.Now.ToString(ConstantValue.DateFormat);


            if (Session["sd"] != null)
            {
                txtStartDate.Value = Session["sd"].ToString();
            }
            if (Session["ed"] != null)
            {
                txtEndate.Value = Session["ed"].ToString();
            }
            if (Session["drpStatus"] != null)
            {
                drpStatus.SelectedValue = Session["drpStatus"].ToString();
            }
            if (IsAdmin)
            {
                panThirdUnit.Visible = true;

                if (Session["ThirdUnit"] != null)
                {
                    drpThirdUnit.SelectedValue = Session["ThirdUnit"].ToString();
                }
            }

            if (Session["drpDept"] != null)
            {
                drpDept.SelectedValue = Session["drpDept"].ToString();
            }
            if (Session["txtDocNo"] != null)
            {
                txtDocNo.Text = Session["txtDocNo"].ToString();
            }
        }
    }


    protected void btnSearch_Click(object sender, EventArgs e)
    {
        Session["sd"] = txtStartDate.Value;
        Session["ed"] = txtEndate.Value;
        Session["drpStatus"] = drpStatus.SelectedValue;
        if (IsAdmin)
        {
            Session["ThirdUnit"] = drpThirdUnit.SelectedValue;
        }
        Session["drpDept"] = drpDept.SelectedValue;
        Session["txtDocNo"] = txtDocNo.Text;
    }

    protected void PageChanged(object sender, DataGridPageChangedEventArgs e)
    {
        if (!IsPostBack)
        {
            BindGridData(e.NewPageIndex);
        }
    }

    protected void BindGridData(int newPageIndex)
    {
        FinReceiptQuery q1 = new FinReceiptQuery("q1");

        TSCodeTableQuery q2 = new TSCodeTableQuery("q2");
        TSCodeTableQuery q3 = new TSCodeTableQuery("q3");
        TSCodeTableQuery q4 = new TSCodeTableQuery("q4");
        TSCodeTableQuery q6 = new TSCodeTableQuery("q6");
        BFThirdUnitQuery q5 = new BFThirdUnitQuery("q5");
        BFCompanyDicQuery q7 = new BFCompanyDicQuery("q7");
        VReceiptExtInfoQuery q8 = new VReceiptExtInfoQuery("q8");


        q1.LeftJoin(q2).On(q1.FunctionDept == q2.Code);
        q1.LeftJoin(q3).On(q1.StorageStatus == q3.Code);
        q1.LeftJoin(q4).On(q1.DocumentsType == q4.Code);
        q1.LeftJoin(q5).On(q1.StorageLocation == q5.ThirdUnitID);
        q1.LeftJoin(q6).On(q1.Division == q6.Code);
        q1.LeftJoin(q7).On(q1.CompanyCode == q7.CompanyCode);
        q1.LeftJoin(q8).On(q1.ReceiptID == q8.ReceiptID);

        q1.SelectAll().Select(q2.Value1.As("FunctionDeptStr"), q3.Value1.As("StorageStatusStr"), q4.Value1.As("DocumentsTypeStr"), q5.Name.As("StorageLocationStr"), q6.Value1.As("DivisionStr"), q7.CompanyName , q7.CompanyCode , q8.SupplierBoxNo, q8.BoxNo);

        if (txtDocNo.Text.Length > 0)
        {
            q1.Where(q1.DocumentsNo.Like("%" + txtDocNo.Text.Trim() + "%") || q1.Reference.Like("%" + txtDocNo.Text.Trim() + "%") || q1.ReferenceKey.Like("%" + txtDocNo.Text.Trim() + "%"));
        }
        if (drpDept.SelectedIndex > 0)
        {
            q1.Where(q1.FunctionDept == drpDept.SelectedValue);
        }
        if (drpStatus.SelectedIndex > 0)
        {
            q1.Where(q1.StorageStatus == drpStatus.SelectedValue);
        }
        if (drpThirdUnit.SelectedIndex > 0)
        {
            q1.Where(q1.StorageLocation == drpThirdUnit.SelectedValue);
        }

        q1.Where(q1.IsConfirmed == false && q1.CreatedDttm >= txtStartDate.Value && q1.CreatedDttm <= DateTime.Parse(txtEndate.Value).AddDays(1));
        q1.OrderBy(q1.CreatedDttm.Descending);

        DataTable tableSorce = q1.LoadDataTable();
        pageGrid.DataSource = tableSorce;
        pageGrid.CurrentPage = newPageIndex;
        pageGrid.DataBind();
    }

    protected override void OnUnload(EventArgs e)
    {
        base.OnUnload(e);
        Session.Remove("sd");
        Session.Remove("ed");
        Session.Remove("drpStatus");
        if (IsAdmin)
        {
            Session.Remove("ThirdUnit");
        }
        Session.Remove("drpDept");
        Session.Remove("txtDocNo");
    }

    protected void btnConfirm_Click(object sender, EventArgs e)
    {
        lbMsg.Text = "";
        try
        {
            using (esTransactionScope scope = new esTransactionScope())
            {
                foreach (RepeaterItem item in pageGrid.Items)
                {
                    CheckBox chkSel = item.FindControl("chkSel") as CheckBox;
                    if (chkSel != null && chkSel.Checked)
                    {
                        HtmlInputHidden repID = item.FindControl("receiptID") as HtmlInputHidden;
                        FinReceipt finR = new FinReceipt();
                        finR.LoadByPrimaryKey(decimal.Parse(repID.Value));
                        finR.IsConfirmed = true;

                        finR.UpdatedDttm = DateTime.Now;
                        finR.UpdatedBy = LoginUserInfo.CurrentUser.LoginName;
                        finR.Save();
                    }

                }
                scope.Complete();

                BindGridData(1);
            }
        }
        catch (Exception ex)
        {
            lbMsg.Text = ex.Message;
        }
    }
}